<?php
/**
 * Created by PhpStorm.
 * User: 17828
 * Date: 2018/10/9
 * Time: 17:06
 */
namespace app\common\model;


use think\Model;

class Contractcar extends Model
{
   public function getReturnCar($field,$where,$page,$limit){
       $list['data'] = db('apply_car')->alias('co')
           ->field($field)
           ->join('car c','c.car_id = co.car_id')
           ->join('store s','c.store_id = s.store_id','left')
           ->join('store st','co.store_id = st.store_id')
           ->join('cartype ct','ct.cartype_id = c.cartype_id')
           ->join('cartype ctp','ctp.cartype_id = c.cartype_ids')
           ->join('member m','m.member_id = co.member_id','left')
           ->where($where)
           ->page($page,$limit)
           ->order('co.apply_car_id desc')
           ->group('co.apply_car_id')
           ->select();
       //echo db()->getLastSql();die;

       $list['count'] = db('apply_car')->alias('co')
           ->field($field)
           ->join('car c','c.car_id = co.car_id')
           ->join('store s','c.store_id = s.store_id','left')
           ->join('store st','co.store_id = st.store_id')
           ->join('cartype ct','ct.cartype_id = c.cartype_id')
           ->join('cartype ctp','ctp.cartype_id = c.cartype_ids')
           ->join('member m','m.member_id = co.member_id','left')
           ->where($where)
           ->group('co.apply_car_id')
           ->count();
       return $list;
   }
   public function getContractCarLog($field,$where,$page,$limit){
       $list['data'] = db('contractcar')->alias('co')
           ->field($field)
           ->join('car c','c.car_id = co.car_id')
           ->where($where)
           ->page($page,$limit)
           ->order('contractcar_id desc')
           ->select();
       $list['count'] = db('contractcar')->alias('co')
           ->join('car c','c.car_id = co.car_id')
           ->where($where)
           ->count();
       return $list;
   }

   public function getDriveWorkLog($field,$where,$page,$limit){
       $list['data'] = db('contractcar')->alias('co')
           ->field($field)
           ->join('car c','co.car_id = c.car_id','left')
           ->join('drive d','d.drive_id = co.drive_id')
           ->join('store s','s.store_id = co.drive_id','left')
           ->where($where)
           ->group('co.contractcar_id')
           ->page($page,$limit)
           ->select();
      // echo db()->getLastSql();die;
       $list['count'] = db('contractcar')->alias('co')
           ->join('car c','co.car_id = c.car_id','left')
           ->join('drive d','d.drive_id = co.drive_id')
           ->join('store s','s.store_id = co.drive_id','left')
           ->where($where)
           ->group('co.contractcar_id')
           ->count();
       return $list;
   }
   public function getCarWorkLog($field,$where,$page,$limit){
       $data = db('contractcar')->alias('co')
           ->field($field)
           ->join('car c','c.car_id = co.car_id')
           ->join('drive d','d.drive_id = co.drive_id','left')
           ->join('store s','s.store_id = d.store_id','left')
           ->where($where)
           ->group('co.contractcar_id')
           ->page($page,$limit)
           ->select();
       $count = db('contractcar')->alias('co')
           ->field($field)
           ->join('car c','c.car_id = co.car_id')
           ->join('drive d','d.drive_id = co.drive_id','left')
           ->join('store s','s.store_id = d.store_id','left')
           ->where($where)
           ->group('co.contractcar_id')
           ->count();
       return ['data'=>$data,'count'=>$count];
   }
   public function getTemporaryContract($field,$where,$page,$limit){
       $data = db('contractcar')->alias('c')
           ->field($field)
           ->join('member m','m.member_id = c.member_id')
           ->join('company cp','cp.company_id = m.company_id')
           ->join('car ca','ca.car_id = c.car_id')
           ->join('store s','ca.store_id = s.store_id')
           ->join('store st','st.store_id = ca.location_store_id')
           ->where($where)
           ->page($page,$limit)
           ->order('c.contractcar_id')
           ->select();
       $count = db('contractcar')->alias('c')
           ->field($field)
           ->join('member m','m.member_id = c.member_id')
           ->join('company cp','cp.company_id = m.company_id')
           ->join('car ca','ca.car_id = c.car_id')
           ->join('store s','ca.store_id = s.store_id')
           ->join('store st','st.store_id = ca.location_store_id')
           ->where($where)
           ->count();
       return ['data'=>$data,'count'=>$count];
   }
   public function getTemporaryContractLog($field,$where,$page,$limit){
       $list['data'] = db('contractcar')->alias('co')
           ->field($field)
           ->join('car c','c.car_id = co.car_id')
           ->join('drive d','d.drive_id = co.drive_id','left')
           ->join('member m','m.member_id = co.member_id')
           ->where($where)
           ->page($page,$limit)
           ->select();
       $list['count'] = db('contractcar')->alias('co')
           ->field($field)
           ->join('car c','c.car_id = co.car_id')
           ->join('drive d','d.drive_id = co.drive_id','left')
           ->join('member m','m.member_id = co.member_id')
           ->where($where)
           ->count();
       return $list;
   }

}